var ctx = "/Home/"

$("#form-user-add").validate({
	rules:{
		loginName:{
			required:true,
			minlength: 2,
			maxlength: 20
		},
		userName:{
			required:true,
		},
		deptName:{
			required:true,
		},
		password:{
			required:true,
			minlength: 5,
			maxlength: 20
		},
		email:{
			required:true,
            email:true
		},
		phonenumber:{
			required:true,
			isPhone:true
		},
	},
	submitHandler:function(form){
		add();
	}
});

function add() {
	var userId = $("input[name='userId']").val();
	var deptId = $("input[name='deptId']").val();
	var loginName = $("input[name='loginName']").val();
	var userName = $("input[name='userName']").val();
	var password = $("input[name='password']").val();
	var email = $("input[name='email']").val();
	var phonenumber = $("input[name='phonenumber']").val();
	var sex = $("#sex option:selected").val();
	var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
	var roleIds = $.getSelects("role");

    var testData = {
        "deptId": deptId,
        "loginName": loginName,
        "userName": userName,
        "password": password,
        "email": email,
        "phonenumber": phonenumber,
        "sex": sex,
        "status": status,
        "roleIds": roleIds
    }

    //alert(JSON.stringify(testData));
    //return false;

	$.ajax({
		cache : true,
		type : "POST",
		url : ctx + "AddUser",
        data: testData,
		async : false,
		error : function(request) {
			$.modalAlert("系统错误", modal_status.FAIL);
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("新增成功,正在刷新数据请稍后……",{icon:1,time: 500,shade: [0.1,'#fff']},function(){
					$.parentReload();
				});
			} else {
				$.modalAlert(data.msg, modal_status.FAIL);
			}

		}
	});
}

/*用户管理-新增-选择部门树*/
function selectDeptTree() {
	var treeId = $("#treeId").val();
	var deptId = treeId == null || treeId == "" ? "100" : treeId;
    var url = ctx + "/treeDepartment/" + deptId;
    layer_show("选择部门", url, '380', '380');
}

$(function () {



})